//
//  ZPositionCell.m
//  WeClubs
//
//  Created by Zed Link on 2/5/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZPositionCell.h"
#import "ZMultiChosePanelCell.h"

NSString * const PositionItemReuseID = @"ZPositionItemReuseID";

@interface ZPositionCell ()

@property (weak, nonatomic) IBOutlet UILabel *indexLabel;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIButton *positionSetBtn;
@property (weak, nonatomic) IBOutlet UIButton *powerSetBtn;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionTop;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionBottom;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionHeight;

/** 职位设置描述 */
@property (strong, nonatomic) NSString *positionDesc;
/** 权限设置描述 */
@property (strong, nonatomic) NSString *powerDesc;

@end

@implementation ZPositionCell

- (void)zSetupCell
{
    [super zSetupCell];
    [self.contentView setBackgroundColor:NAVBAR_BG_COLOR];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [self.indexLabel zCornerDeal:8];
    
    [self.positionSetBtn addTarget:self action:@selector(positionBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.powerSetBtn addTarget:self action:@selector(powerBtnAction) forControlEvents:UIControlEventTouchUpInside];

    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake(60, 24);
    layout.minimumLineSpacing = 8;
    layout.minimumInteritemSpacing = 8;

    [self.collectionView setBackgroundColor:[UIColor clearColor]];
    [self.collectionView registerClass:[ZMultiChosePanelItem class] forCellWithReuseIdentifier:PositionItemReuseID];
}

#pragma mark - Control Action

- (void)positionBtnAction
{
    if (self.didSelectPositionChose) {
        self.didSelectPositionChose();
    }
}

- (void)powerBtnAction
{
    if (self.didSelectPowerChose) {
        self.didSelectPowerChose();
    }
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.powerDataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZMultiChosePanelItem *item = [collectionView dequeueReusableCellWithReuseIdentifier:PositionItemReuseID forIndexPath:indexPath];
    item.state = ZMultiChoseStateSelected;
    [item.button zCornerDeal:12];
    [item setTitle:self.powerDataSource[indexPath.item] andFontSize:12];
    
    return item;
}

#pragma mark - Getter & Setter

- (void)setIndex:(NSInteger)index
{
    self.indexLabel.text = [NSString stringWithFormat:@"%zd", index];
}

- (void)setPosition:(NSString *)position
{
    if ([position isEqualToString:@""]) {
        NSDictionary *attr = @{
                               NSFontAttributeName : [UIFont systemFontOfSize:14],
                               NSForegroundColorAttributeName : FONT_THIRD_COLOR,
                               };
        NSAttributedString *title = [[NSAttributedString alloc] initWithString:self.positionDesc attributes:attr];
        
        self.positionSetBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [self.positionSetBtn setAttributedTitle:title forState:UIControlStateNormal];
    }
    else {
        NSDictionary *attr = @{
                               NSFontAttributeName : [UIFont systemFontOfSize:14],
                               NSForegroundColorAttributeName : FONT_MAIN_COLOR,
                               };
        NSAttributedString *title = [[NSAttributedString alloc] initWithString:position attributes:attr];
        
        [self.positionSetBtn setAttributedTitle:title forState:UIControlStateNormal];
        self.positionSetBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    }
}

- (void)setPowerDataSource:(NSArray *)powerDataSource
{
    _powerDataSource = powerDataSource;
    
    if (_powerDataSource.count > 0) {
        self.state = ZPositionStateConfigurated;
    } else {
        self.state = ZPositionStateUnset;
    }
}

- (void)setState:(ZPositionState)state
{
    _state = state;
    
    CGFloat height;
    switch (_state) {
        case ZPositionStateUnset:
        {
            [self.collectionView setHidden:YES];
            [self.powerSetBtn setTitle:self.powerDesc forState:UIControlStateNormal];
            
            // 显示Label时使用固定高度
            height = 37 - _collectionTop.constant - _collectionBottom.constant;
        }
            break;
       case ZPositionStateConfigurated:
        {
            [self.collectionView setHidden:NO];
            [self.powerSetBtn setTitle:nil forState:UIControlStateNormal];
            
            // 刷新collectionView, 由CollectionView自动计算内容的高度
            [self.collectionView reloadData];
            height = self.collectionView.collectionViewLayout.collectionViewContentSize.height;
        }
            break;
        default:
            break;
    }
    
    // 视图被隐藏时约束也会生效, 依然能撑开cell的高度, 实现 self-sizing
    self.collectionHeight.constant = height;
}

- (NSString *)positionDesc
{
    if (_positionDesc == nil) {
        _positionDesc = @"点击选择职位";
    }
    return _positionDesc;
}

- (NSString *)powerDesc
{
    if (_powerDesc == nil) {
        _powerDesc = @"点击设置权限";
    }
    return _powerDesc;
}

@end
